import KoaRouter from 'koa-router'
import Config from '@/config/Config'
import verifyToken from '@/middlewares/verifyToken'
import { Models } from '@/common/typings/modle'
import { command } from '@/server/mysql'
import { excelColumns } from '@/common/constants/system/value-set'
import { formatWhere } from '@/common/utils/sql'
import commonExport from '@/api/v1/common/file/export'
const router = new KoaRouter({
  prefix: `${Config.API_PREFIX}v1/system/valueSet`,
})
router.post(
  '/export',
  verifyToken,
  commonExport({
    async getResult(ctx: Models.Ctx) {
      const { setCode, setName, ids } = ctx.request.body as any
      let where = ''
      if (ids?.length) {
        // 根据ids查询数据
        where = `WHERE 1=1 AND id IN (${ids.map((item: string | number) => `'${item}'`).join()})`
      } else {
        // 根据查询条件查询数据
        where = `WHERE 1=1 ${setCode ? `AND set_code LIKE '%${setCode}%'` : ``} ${
          setName ? `AND set_name LIKE '%${setName}%'` : ``
        }`
      }
      where = formatWhere(where)
      return (await command(`SELECT set_code, set_name, code, name FROM system_value_set ${where}`, true)).results
    },
    fileName: '值集列表',
    excelColumns,
  }),
)
export default router
